package groovy

import com.gee.spring.cloud.common.core.domain.frame.CstpWrapper
import com.gee.spring.cloud.common.core.entity.center.config.Org
import com.gee.spring.cloud.common.core.entity.center.config.OrgCenter
import com.gee.spring.cloud.data.transform.core.util.MappingUtil
import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufUtil
import io.netty.buffer.PooledByteBufAllocator

static byte[] toOuter(Org org, OrgCenter orgCenter, CstpWrapper cstpWrapper){
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer()
    byteBuf.writeByte(0x10)
        .writeShortLE((short)MappingUtil.getMappingSateIdentity(orgCenter.getOrgId(), cstpWrapper.getTid()))      //tid
        .writeIntLE((int)MappingUtil.getMappingDevIdentity(orgCenter.getOrgId(), cstpWrapper.getSid()))           //sid
        .writeIntLE((int)MappingUtil.getMappingOrgCenterIdentity(orgCenter.getOrgId(), orgCenter.getCenterIdentity()))     //did
        .writeIntLE((int)MappingUtil.getMappingBid(orgCenter.getOrgId(), cstpWrapper.getBid()))                   //bid
        .writeIntLE((int)cstpWrapper.getPackageNo())
        .writeByte(0x00)
        .writeIntLE(0x00000000)
        .writeShortLE((int)cstpWrapper.getDate())
        .writeIntLE((int)cstpWrapper.getTime())
        .writeShortLE((int)cstpWrapper.getLength())
        .writeBytes(cstpWrapper.getDataArea())
    byte[] bytes = ByteBufUtil.getBytes(byteBuf)
    byteBuf.release();
    return bytes;
}
